先来点:
#----- 1v1 = [1,2,3,4]v1.append(123)print(v1) #[1, 2, 3, 4, 123]#-----2v1 = { 'k1':'v1'}v1['k2'] = 'v2'print(v1) #3{'k1': 'v1', 'k2': 'v2'}#------3data = [ [11,22,33], [44,55,66]]print('先',data)data[0].append(data[1])print('后',data)# 先 [[11, 22, 33], [44, 55, 66]]# 后 [[11, 22, 33, [44, 55, 66]], [44, 55, 66]]
再来点:
data = [ [11,22,33], [44,55,66]]data[0].append(data[1]) #前data[1].append(77) #中print('后',data[0][3]) #后# 前 [[11, 22, 33, [44, 55, 66]], [44, 55, 66]]# 中 [[11, 22, 33, [44, 55, 66, 77]], [44, 55, 66, 77]]# 后 [44, 55, 66, 77]
不急,再来点:
data = [ { 'k1':'v1'}, { 'k2':'v2'}]for item in data: item['kk'] = 'vv'print(data)#[{'k1': 'v1', 'kk': 'vv'}, {'k2': 'v2', 'kk': 'vv'}]
不能再来了,来点爽快的:
msg_list = [ {'id':1,'content':'xxx','parent_id':None}, {'id':2,'content':'xxx','parent_id':None}, {'id':3,'content':'xxx','parent_id':None}, {'id':4,'content':'xxx','parent_id':1}, {'id':5,'content':'xxx','parent_id':4}, {'id':6,'content':'xxx','parent_id':2}, {'id':7,'content':'xxx','parent_id':5}, {'id':8,'content':'xxx','parent_id':3},]"""## ---- 前戏# v = [row.setdefault('child',[]) for row in msg_list] #这个写法 好像 有点难# print(msg_list)for item in msg_list: #换个 简单一点点 item['child'] = []print('***',msg_list)"""msg_list_dict = {}for item in msg_list: item['child'] = [] msg_list_dict[item['id']] = itemresult = []for item in msg_list: pid = item['parent_id'] if pid: msg_list_dict[pid]['child'].append(item) else: result.append(item)print(result)""" **********************展示效果**************************[ {'id': 1, 'content': 'xxx', 'parent_id': None, 'child': [ {'id': 4, 'content': 'xxx', 'parent_id': 1, 'child': [ {'id': 5, 'content': 'xxx', 'parent_id': 4, 'child': [ {'id': 7, 'content': 'xxx', 'parent_id': 5, 'child': []}]}]}]}, {'id': 2, 'content': 'xxx', 'parent_id': None, 'child': [ {'id': 6, 'content': 'xxx', 'parent_id': 2, 'child': []}]}, {'id': 3, 'content': 'xxx', 'parent_id': None, 'child': [ {'id': 8, 'content': 'xxx', 'parent_id': 3, 'child': []}]}]"""
评论以及多级评论
views.py
def comment(request): #评论 user_id = request.user.nid article_id = request.POST.get('article_id') comment_content =request.POST.get('comment_content') # print('%%%%%%%%%', comment_content) if request.POST.get('parent_comment_id'): c = int(request.POST.get('parent_comment_id')) comment_obj = models.Comment.objects.create(article_id=article_id, content=comment_content, user_id=user_id, parent_id_id=c) else: comment_obj = models.Comment.objects.create(article_id=article_id, content=comment_content,user_id=user_id) models.Article.objects.filter(nid=article_id).update(comment_count=F("comment_count")+1) response_ajax = { "comment_createTime":str(comment_obj.create_time)[:16]} return HttpResponse(json.dumps(response_ajax))
.HTML
{% block article %}{ { article_obj.title }}
{ { article_detail.content|safe }}
{% csrf_token %} 评论 赞({ { article_obj.up_count }}){ { user_obj.nickname }} 发布于{ { article_obj.create_time }} 阅读({ { article_obj.read_count }}) 评论({ { article_obj.comment_count }})
已有评论
- {% for coment in coment_list %}
- { { coment.user.username }} { { coment.create_time|date:"Y-m-d H:i" }} 支持 回复 {% if coment.parent_id %} @{ { coment.parent_id.user.username }}:{ { coment.parent_id.content }}
{
{ coment.content }} {% else %} { { coment.content }} {% endif %} {% endfor %}